package net.crawwle.career.analyze.dao;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;

/**
 *
 * @author hakdogan
 */

public class JobSearchDao {



    public static void main(String args[]) throws IOException{
    	JobSearchDao dao = new JobSearchDao();
    	Client es = dao.getElasticSearchClient();
    	SearchHit[] hists = dao.searchDocument(es, "jobs", "couchbaseDocument","AIU損害保険株式会社");
    	System.out.println(hists.length);
    }


	private Node node = null;

	private Client client = null;

	public Client getElasticSearchClient() {
		if (this.node == null) {
			node = NodeBuilder.nodeBuilder().node();
		}

		if (this.client == null) {
			client = node.client();
		}
		return this.client;
	}

	public void closeElasticSearchClient() {
		if (this.client != null) {
			client.close();
		}
		if (this.node != null) {
			node.close();
		}
	}

	public Map<String, Object> putJsonDocument(String title, String content,
			Date postDate, String[] tags, String author) {

		Map<String, Object> jsonDocument = new HashMap<String, Object>();

		jsonDocument.put("title", title);
		jsonDocument.put("content", content);
		jsonDocument.put("postDate", postDate);
		jsonDocument.put("tags", tags);
		jsonDocument.put("author", author);

		return jsonDocument;
	}

	public void getDocument(Client client, String index, String type, String id) {

		GetResponse getResponse = client.prepareGet(index, type, id).execute()
				.actionGet();
		Map<String, Object> source = getResponse.getSource();

		System.out.println("------------------------------");
		System.out.println("Index: " + getResponse.getIndex());
		System.out.println("Type: " + getResponse.getType());
		System.out.println("Id: " + getResponse.getId());
		System.out.println("Version: " + getResponse.getVersion());
		System.out.println(source);
		System.out.println("------------------------------");

	}

	public void updateDocument(Client client, String index, String type,
			String id, String field, String newValue) {

		Map<String, Object> updateObject = new HashMap<String, Object>();
		updateObject.put(field, newValue);

		client.prepareUpdate(index, type, id)
				.setScript("ctx._source." + field + "=" + field)
				.setScriptParams(updateObject).execute().actionGet();
	}

	public void updateDocument(Client client, String index, String type,
			String id, String field, String[] newValue) {

		String tags = "";
		for (String tag : newValue)
			tags += tag + ", ";

		tags = tags.substring(0, tags.length() - 2);

		Map<String, Object> updateObject = new HashMap<String, Object>();
		updateObject.put(field, tags);

		client.prepareUpdate(index, type, id)
				.setScript("ctx._source." + field + "+=" + field)
				.setScriptParams(updateObject).execute().actionGet();
	}

	public SearchHit[] searchDocument(Client client, String index, String type,
			String value) {

		SearchResponse response = client.prepareSearch(index).setTypes(type)
				.setSearchType(SearchType.QUERY_THEN_FETCH)
				.setQuery(QueryBuilders.queryString(value))//.setFrom(0)
				.setSize(60).setExplain(true).execute().actionGet();

		SearchHit[] results = response.getHits().getHits();

		/*System.out.println("Current results: " + results.length);
		for (SearchHit hit : results) {
			System.out.println("------------------------------");
			Map<String, Object> result = hit.getSource();
			System.out.println(result);
		}*/
		return results;
	}

	public  SearchHit[] searchDocument(Client client, String index, String type,
			String value1,String value2) {

		QueryBuilder qb = QueryBuilders
                .boolQuery()
                .must(QueryBuilders.queryString(value1))
                .must(QueryBuilders.queryString(value2));

		SearchResponse response = client.prepareSearch(index).setTypes(type)
				.setSearchType(SearchType.QUERY_THEN_FETCH)
				.setQuery(qb)//.setFrom(0)
				.setSize(60).setExplain(true).execute().actionGet();

		SearchHit[] results = response.getHits().getHits();

		/*System.out.println("Current results: " + results.length);
		for (SearchHit hit : results) {
			System.out.println("------------------------------");
			Map<String, Object> result = hit.getSource();
			System.out.println(result);
		}*/
		return results;
	}


	public  SearchHit[] searchAllDocument(Client client,String index, String type) {

		SearchResponse response = client.prepareSearch(index).setTypes(type)
				.setSearchType(SearchType.QUERY_THEN_FETCH)
				.setQuery(QueryBuilders.matchAllQuery())//.setFrom(0)
				.setSize(10).setExplain(true).execute().actionGet();

		SearchHit[] results = response.getHits().getHits();

		/*System.out.println("Current results: " + results.length);
		for (SearchHit hit : results) {
			System.out.println("------------------------------");
			Map<String, Object> result = hit.getSource();
			System.out.println(result);
		}*/
		return results;
	}


	public void deleteDocument(Client client, String index, String type,
			String id) {

		DeleteResponse response = client.prepareDelete(index, type, id)
				.execute().actionGet();
		System.out.println("Information on the deleted document:");
		System.out.println("Index: " + response.getIndex());
		System.out.println("Type: " + response.getType());
		System.out.println("Id: " + response.getId());
		System.out.println("Version: " + response.getVersion());
	}
}
